package com.sum.tea.xinlian

import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Toast
import com.blankj.utilcode.util.ToastUtils
import com.function.commfun
import com.sum.common.base.BaseMvvmActivity
import com.sum.tea.contants.AppConfig
import com.sum.tea.databinding.ActivityTagtempledBinding
import com.sum.tea.xinlian.bean.LEDParams
import com.sum.tea.xinlian.bean.TemperatureParams
import com.sum.tea.xinlian.viewmodel.TagTempViewModel
import com.uhf.api.cls.R2000_calibration
import com.uhf.api.cls.Reader
import com.uhf.api.cls.Reader.TagFilter_ST

class SubTagTempLedActivity : BaseMvvmActivity<ActivityTagtempledBinding, TagTempViewModel>(){
    private lateinit var myapp: AppConfig
    private val YLtemperture = 45

    override fun initView(savedInstanceState: Bundle?) {
        myapp = AppConfig()
        mBinding.spinnerOpfbankTtl.adapter = ArrayAdapter(this@SubTagTempLedActivity, android.R.layout.simple_spinner_item, AppConfig.spifbank)
        mBinding.spinnerBankrTtl.adapter = ArrayAdapter(this@SubTagTempLedActivity, android.R.layout.simple_spinner_item, AppConfig.spibank)
        mBinding.spinnerBankrTtl.setSelection(3)

        // Set onClickListeners
        mBinding.buttonReadTtl.setOnClickListener { mViewModel.readTemperature(getTemperatureParams()) }
        mBinding.buttonLedTtl.setOnClickListener { toggleLED() }
        observeViewModel()
    }

    private fun observeViewModel() {
        mViewModel.temperatureData.observe(this) { result ->
            result?.let {
                mBinding.editTextDatarTtl.setText(it)
            }
        }
        mViewModel.ledData.observe(this) { result ->
            result?.let {
                mBinding.editTextDatarTtl.setText(it)
            }
        }
        mViewModel.toastMessage.observe(this) { message ->
            message?.let {
                ToastUtils.showShort(it)
            }
        }
    }

    private fun toggleLED() {
        val txt = mBinding.buttonLedTtl.text.toString().trim()
        if (txt == "读LED") {
            val params = getLEDParams()
            mViewModel.startLEDTask(params)
            mBinding.buttonLedTtl.text = "停止"
        } else {
            mViewModel.stopLEDTask()
            mBinding.buttonLedTtl.text = "读LED"
        }
    }

    private fun getTemperatureParams(): TemperatureParams {
        return TemperatureParams(
            opant = commfun.SortGroup(mBinding.radioGroupOpantTtl) + 1,
            password = if (mBinding.checkBoxOpacepwdTtl.isChecked) mBinding.editTextPasswordTtl.text.toString() else "",
            filterData = getFilterData(),
            bank = mBinding.spinnerBankrTtl.selectedItemPosition.toChar(),
            adrhex = mBinding.editTextStartaddrTtl.text.toString().toInt(16),
            blkcnt = mBinding.editTextOpcountrTtl.text.toString().toInt(),
            selwait = mBinding.editTextWaitselTtl.text.toString().toInt(),
            readwait = mBinding.editTextWaitreadTtl.text.toString().toInt(),
            readtimeout = mBinding.editTextInvtimeTtl.text.toString().toInt(),
            metaData = R2000_calibration().META_DATA()
        )
    }

    private fun getLEDParams(): LEDParams {
        return LEDParams(
            opant = commfun.SortGroup(mBinding.radioGroupOpantTtl) + 1,
            sleep = mBinding.editTextLedttl.text.toString().toInt(),
            readtimeout = mBinding.editTextInvtimeTtl.text.toString().toInt(),
            metaData = R2000_calibration().META_DATA().apply {
                IsReadCnt = mBinding.checkBoxReadcountTtl.isChecked
                IsRSSI = mBinding.checkBoxRssiTtl.isChecked
                IsAntennaID = mBinding.checkBoxAntTtl.isChecked
                IsFrequency = mBinding.checkBoxFrequencyTtl.isChecked
                IsTimestamp = mBinding.checkBoxTimeTtl.isChecked
                IsRFU = mBinding.checkBoxRfuTtl.isChecked
                IsPro = mBinding.checkBoxProTtl.isChecked
                IsEmdData = mBinding.checkBoxTagdatalenTtl.isChecked
            }
        )
    }

    private fun getFilterData(): TagFilter_ST? {
        return if (commfun.SortGroup(mBinding.radioGroupEnableopfilTtl) == 1) {
            val et = mBinding.editTextOpfilterdataTtl
            val ln = et.text.length.let { if (it == 1 || it % 2 == 1) it + 1 else it }
            val fdata = ByteArray(ln / 2)
            myapp.Mreader.Str2Hex(et.text.toString(), ln, fdata)
            Reader().TagFilter_ST().apply {
                this.fdata = fdata
                this.flen = et.text.length * 4
                this.isInvert = if (commfun.SortGroup(mBinding.radioGroupOpmatchTtl) == 1) 1 else 0
                this.bank = mBinding.spinnerOpfbankTtl.selectedItemPosition + 1
                this.startaddr = mBinding.editTextOpfilsadrTtl.text.toString().toInt()
            }
        } else {
            null
        }
    }
}

